<template>
	<view>
		<uv-loading-page :loading="loading" loading-text="加载中..." font-size="24rpx"></uv-loading-page>
		<view>
			<view v-show="pageList[value]">
				<component :is="pageList[value].page"></component>
			</view>
			<uv-tabbar :value="value" :fixed="true" @change="change" :border="false">
				<uv-tabbar-item v-for="(item,index) in pageList" :key="index" :text="item.title"
					:icon="item.icon"></uv-tabbar-item>
			</uv-tabbar>
		</view>
	</view>
</template>
<script setup>
	import {
		version
	} from '../../../package.json'
	import {
		compareVersions,
		shouldUpdate,
		showUpdateDialog
	} from '@/untils/version.js';
	import {
		ref,
		markRaw,
		onMounted,
	} from 'vue'
	import {
		onShow,
		onLoad,
	}
	from '@dcloudio/uni-app'
	import homeVue from '../home/home.vue'
	import workVue from '../work/work.vue'
	import merchantVue from '../merchant/merchant.vue'
	import dataVue from '../data/data.vue'
	import messageVue from '../message/message.vue'
	import mineVue from '../mine/mine.vue'

	import {
		getVersionApi
	} from '@/api/utils.js'
	const value = ref(0)
	const loading = ref(true)

	onMounted(() => {
		setTimeout(() => {
			loading.value = false
		}, 1000);
		getVersion()
	})

	// 版本号
	let versionCode = ref('1.0.0')
	const getVersion = async () => {
		try {
			let res = await getVersionApi();
			versionCode.value = res.data.agencyVersion;
			if (shouldUpdate(version, versionCode.value)) {
				showUpdateDialog(versionCode.value, () => {
					console.log('用户点击确定，执行更新操作');
					// 这里可以调用下载更新方法
				}, () => {
					console.log('用户点击取消');
				});
			}
		} catch (error) {
			console.error('获取版本信息失败:', error);
		}
	};

	// tabbar列表
	const pageList = ref([{
			icon: 'grid',
			title: '工作台',
			page: markRaw(workVue),
		}, {
			icon: 'bag',
			title: '商户',
			page: markRaw(merchantVue),
		}, {
			icon: 'order',
			title: '数据',
			page: markRaw(dataVue),
		}, {
			icon: 'chat',
			title: '消息',
			page: markRaw(messageVue),
		},
		{
			icon: 'account',
			title: '我的',
			page: markRaw(mineVue),
		}
	])
	const change = (index) => {
		value.value = index
		loading.value = true
		setTimeout(() => {
			loading.value = false
		}, 1000);
	}
</script>

<style>

</style>